<?php

namespace Model;

class RegisterHandler{
	
	private $tablename = "login";
	private $db = NULL;
	
	public function __construct(Database $Database) {
		$this->db = $Database;
	}
	
	// Kontrollerar om det inskriva användarnamnet redan finns i databasen
	public function DoesUserExist($username){
				
		$sql = "SELECT COUNT(*) FROM $this->tablename WHERE username = ?";
		
		$stmt = $this->db->Prepare($sql);
		
		$stmt->bind_param("s", $username);
		
		$this->db->Execute($stmt);
		
		if($this->db->SearchForUser($stmt) == 1){
			return true;
		}
	}
	
	// Lägger till den nya användaren i databasen
	public function RegisterUser($username, $password){
				
		if (strlen($password) < 6) {
			return false;
		}
		
		$sql = "INSERT INTO $this->tablename (username, password) VALUES (?, ?)";
		
		$stmt = $this->db->Prepare($sql);
		
		$stmt->bind_param("ss", $username, $password);
		
		
		if($this->db->Execute($stmt)){
			$stmt->close();
			return true;
		}		
		$stmt->close();
		return false;		
	}
	
	// Tar bort en vala användare
	public function RemoveUser($username){
		
		$sql = "DELETE FROM $this->tablename WHERE username = ?";
		
		$stmt = $this->db->Prepare($sql);
		
		$stmt->bind_param("s", $username);
		
		if($this->db->Execute($stmt)){
			$stmt->close();
			return true;
		}
		$stmt->close();
		return false;
	}
}